#include<bits/stdc++.h>
using namespace std;
#define int long long
int read(){
    int n=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        n=n*10+ch-'0';
        ch=getchar();
    }
    return n*f;
}
int n,m;
int ap[200005];
int a[200005];
int c[200005];
void inp(){
    vector<int> x(4);
    for(int i=0;i<4;i++)x[i]=read();
    for(int i=1;i<=n;i++)a[i]=ap[i]^x[i%4];
}
char s[25][200005];
int len[25];
int ls[200005],rs[200005];
int ch[200005];
vector<int> g[200005];
int rnd[200005];
void dfs(int x){
    if(!ls[x]&&!rs[x]){
        return;
    }
    if(ls[x])dfs(ls[x]);
    if(rs[x])dfs(rs[x]);
    if(!ch[x]){
        //choose from ls[x].
        bool ok=0;
        for(int i=0;i<(int)g[ls[x]].size();i++){
            int v=g[ls[x]][i];
            if(a[v]>=rnd[x]){
                g[x].push_back(v);
            }
            else{
                ok=1;
            }
        }
        if(ok){
            for(int i=0;i<(int)g[rs[x]].size();i++)g[x].push_back(g[rs[x]][i]);
        }
    }
    else{
        bool ok=0;
        for(int i=0;i<(int)g[rs[x]].size();i++){
            int v=g[rs[x]][i];
            if(a[v]>=rnd[x]){
                g[x].push_back(v);
            }
            else{
                ok=1;
            }
        }
        if(ok){
            for(int i=0;i<(int)g[ls[x]].size();i++)g[x].push_back(g[ls[x]][i]);
        }
    }
}
vector<pair<int,int>> que[25];
void work(){
    int k=0;while((1ll<<k)<n)++k;
    int res=0;
    for(int p=0;p<=k;p++){
        if(!p){
            for(int j=0;j<(int)que[p].size();j++){
                int id=que[p][j].second;
                res^=id;
            }
            continue;
        }
        for(int j=1;j<=(1ll<<(p+1));j++)ls[j]=rs[j]=0,ch[j]=-1,rnd[j]=-1;
        int tot=(1ll<<p);
        vector<int> sus;
        for(int j=1;j<=(1ll<<p);j++)sus.push_back(j);
        for(int j=1;j<=p;j++){
            vector<int> tmp;
            for(int k=0;k<(int)sus.size();k+=2){
                ++tot;
                ls[tot]=sus[k],rs[tot]=sus[k+1];tmp.push_back(tot);
                rnd[tot]=j;
                ch[tot]=s[j][k/2+1]-'0';
            }
            sus=tmp;
        }
        for(int k=1;k<=tot;k++)g[k].clear();
        //root=tot.
        for(int k=1;k<=(1ll<<p);k++)g[k].push_back(k);
        dfs(tot);
        //cout<<"WIN"<<g[ls[tot]].size()<<" "<<g[ls[tot]][0]<<"\n";
        for(int j=0;j<(int)que[p].size();j++){
            int id=que[p][j].second,li=que[p][j].first;
            int cur=0;
            for(int k=0;k<(int)g[tot].size();k++)cur+=g[tot][k];
            //cout<<"W"<<cur<<" "<<id<<"\n";
            res^=(id*cur);
        }
    }
    cout<<res<<"\n";
}
void merge(int x,int li){
    g[x].clear();
    if(!ch[x]){
        //choose from ls[x].
        bool ok=0;
        for(int i=0;i<(int)g[ls[x]].size();i++){
            int v=g[ls[x]][i];
            if(v<=li&&a[v]>=rnd[x]){
                g[x].push_back(v);
            }
            else{
                ok=1;
                if(v>li)g[x].push_back(v);
            }
        }
        if(ok){
            for(int i=0;i<(int)g[rs[x]].size();i++)g[x].push_back(g[rs[x]][i]);
        }
    }
    else{
        bool ok=0;
        for(int i=0;i<(int)g[rs[x]].size();i++){
            int v=g[rs[x]][i];
            if(v<=li&&a[v]>=rnd[x]){
                g[x].push_back(v);
            }
            else{
                ok=1;
                if(v>li)g[x].push_back(v);
            }
        }
        if(ok){
            for(int i=0;i<(int)g[ls[x]].size();i++)g[x].push_back(g[ls[x]][i]);
        }
    }
}
void solve(){
    int k=0;while((1ll<<k)<n)++k;
    int res=0;
    for(int p=0;p<=k;p++){
        if(!p){
            for(int j=0;j<(int)que[p].size();j++){
                int id=que[p][j].second;
                res^=id;
            }
            continue;
        }
        for(int j=1;j<=(1ll<<(p+1));j++)ls[j]=rs[j]=0,ch[j]=-1,rnd[j]=-1;
        int tot=(1ll<<p);
        vector<int> sus;
        for(int j=1;j<=(1ll<<p);j++)sus.push_back(j);
        for(int j=1;j<=p;j++){
            vector<int> tmp;
            for(int k=0;k<(int)sus.size();k+=2){
                ++tot;
                ls[tot]=sus[k],rs[tot]=sus[k+1];tmp.push_back(tot);
                rnd[tot]=j;
                ch[tot]=s[j][k/2+1]-'0';
            }
            sus=tmp;
        }
        for(int k=1;k<=tot;k++)g[k].clear();//ww[k].clear();
        //root=tot.
        for(int k=1;k<=(1ll<<p);k++)g[k].push_back(k);
        dfs(ls[tot]);
        //dfs2(rs[tot]);
        //cout<<"WIN"<<g[ls[tot]].size()<<" "<<g[ls[tot]][0]<<"\n";
        for(int j=0;j<(int)que[p].size();j++){
            int id=que[p][j].second,li=que[p][j].first;
            //ww[li].push_back(id);
            vector<int> gp;int cc=(1ll<<p)+1;
            for(int x=1;x<=p;x++){
                if(x!=p)cc+=(1<<(p-x-1));
                int lim=cc;
                if(x!=p)lim+=(1<<(p-x-1))-1;
                for(int y=cc;y<=lim;y++)gp.push_back(y);
                if(x!=p)cc+=(1<<(p-x-1));
            }
            for(int id=0;id<(int)gp.size();id++){
                int x=gp[id];
                merge(x,li);
            }//cout<<"\n";
            int cur=0;
            for(int k=0;k<(int)g[tot].size();k++)cur+=g[tot][k];
            //cout<<"W"<<cur<<" "<<id<<"\n";
            res^=(id*cur);
        }
    }/*
    int res=0;
    for(int i=1;i<=m;i++){
        int p=0;
        while((1ll<<p)<c[i])p++;
        for(int j=1;j<=(1ll<<p);j++)ls[j]=rs[j]=0,ch[j]=-1,rnd[j]=-1;
        int tot=(1ll<<p);
        vector<int> sus;
        for(int j=1;j<=(1ll<<p);j++)sus.push_back(j);
        for(int j=1;j<=p;j++){
            vector<int> tmp;
            for(int k=0;k<(int)sus.size();k+=2){
                ++tot;
                ls[tot]=sus[k],rs[tot]=sus[k+1];tmp.push_back(tot);
                rnd[tot]=j;
                ch[tot]=s[j][k/2+1]-'0';
            }
            sus=tmp;
        }
        for(int j=1;j<=tot;j++)g[j].clear();
        //root=tot.
        dfs(tot,c[i]);
        int cur=0;
        for(int j=0;j<(int)g[tot].size();j++)cur+=g[tot][j];
        res^=(i*cur);
    }*/
    cout<<res<<"\n";
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=n;i++)ap[i]=read();
    for(int i=1;i<=m;i++)c[i]=read();
    bool fval=1;
    for(int i=1;i<=m;i++){
        int p=0;
        while((1ll<<p)<c[i])p++;
        fval&=((1ll<<p)==c[i]);
        que[p].push_back({c[i],i});
    }
    int k=0;
    while((1ll<<k)<n)k++;
    for(int i=1;i<=k;i++){
        scanf("%s",s[i]+1);len[i]=strlen(s[i]+1);
    }
    //if(fval){puts("qwerty");}
    int t=read();
    while(t--){
        inp();
        if(fval)work();
        else solve();
    }
    return 0;
}
